package synchronization

import "goDFS/public"

// 这里添加同步方法

//同步结构
type SyncFileAddress struct {
	*public.StorageAddr
	Key string
}

//新建一个同步用地址令牌
func NewSyncFileAddress(ip string, port string, key string) *SyncFileAddress {
	l := public.NewStorageAddr(ip, port)
	if l == nil {
		return nil
	}

	return &SyncFileAddress{
		l,
		key,
	}
}

//同步管理器
type SyncManager struct {
	SyncChan chan *SyncFileAddress //用来完成同步
}

//创建一个同步管理器
func NewSyncManage() *SyncManager {
	syncChan := make(chan *SyncFileAddress, 100)
	return &SyncManager{
		SyncChan: syncChan,
	}
}

//开始同步服务
func (l *SyncManager) StartSync(addrList []*public.StorageAddr) {

	for true {
		select {
		case SynAddr := <-l.SyncChan:
			go SendSyncMsg(SynAddr, addrList)
		}
	}
}

//添加一个同步请求
func (l *SyncManager) AddSyncFile(sync *SyncFileAddress) {
	l.SyncChan <- sync
}
